package com.rzico.quartz.job;

import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Supplier;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.SupplierService;
import com.rzico.core.entity.SysJob;
import com.rzico.core.service.SysJobService;
import com.rzico.order.entity.Return;
import com.rzico.order.plugin.JDPluginService;
import com.rzico.order.service.ReturnService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 京东服务单退货单填写物流信息，更新退货单信息
 */
public class JDTrackApplyTask implements Job {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected SysJobService sysJobService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private ReturnService returnService;

    @Autowired
    protected JDPluginService jDPluginService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String jobId = jobExecutionContext.getTrigger().getKey().getName();
        SysJob sysJob = sysJobService.selectByPrimaryKey(jobId);
        try {
            if (sysJob.getExecStatus().equals(1)) {
                //不能重复执行
                return;
            }
            sysJob.setExecStatus(1);
            sysJob.setJobDesc("开始执行:"+sdf.format(new Date()));
            sysJobService.updateByPrimaryKeySelective(sysJob);

            // 这里开始
            //获取商户号
            String mchId = "";
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("name","葫芦派");
            List<Enterprise> enterpriseList= enterpriseService.selectList(param);
            if (0 < enterpriseList.size()) {
                mchId = enterpriseList.get(0).getMchId();
            }
            //获取京东供应商id
            Long supplierId = 0L;
            Long enterpriceId = 0L;
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("name","京东");
            List<Supplier> supplierList = supplierService.selectList(params);
            if (0 < supplierList.size()) {
                supplierId = supplierList.get(0).getId();
                enterpriceId = supplierList.get(0).getEnterpriseId();
            }
            Map<String,Object> returnParams = new HashMap<String, Object>();
            returnParams.put("shippingStatus", 1);
            returnParams.put("supplierId", supplierId);
            List<Return> returnList = returnService.selectList(returnParams);
            //根据申请退货单，填写发运信息
            jDPluginService.trackApply(returnList, supplierId, mchId);
            //更新退货单状态
            jDPluginService.updateReturnStatus(returnList);
            
            // end

            sysJob.setExecStatus(0);
            sysJob.setJobDesc("执行成功:"+sdf.format(new Date())+",下次执行:"+sdf.format(jobExecutionContext.getNextFireTime()));
            sysJobService.updateByPrimaryKeySelective(sysJob);
        }
        catch (Exception e) {
            sysJob.setExecStatus(0);
            sysJob.setJobDesc("执行错误:"+sdf.format(new Date()));
            sysJobService.updateByPrimaryKeySelective(sysJob);
        }

    }
}
